package com.chapter13.ArrayExercise;

import java.util.Comparator;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
public class ArrayExercise01 {
    public static void main(String[] args) {
        Book[] books = new Book[4];
        books[0] = new Book("红楼梦", 100);
        books[1] = new Book("金瓶梅新", 90);
        books[2] = new Book("青年文摘20年", 5);
        books[3] = new Book("java从入门到放弃", 300);
        AA(books, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                int i1 = (Integer) o1;
                int i2 = (Integer) o2;
                return i1 - i2;
            }
        });
        for (int i = 0; i < books.length; i++) {
            System.out.println(books[i]);
        }
        BB(books, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                int i1 = (Integer) o1;
                int i2 = (Integer) o2;
                return i1 - i2;
            }
        });
        System.out.println("=======================================");
        for (int i = 0; i < books.length; i++) {
            System.out.println(books[i]);
        }
    }
    public static void BB(Book[] books,Comparator c){
        Book temp;
        for (int i = 0; i < books.length - 1; i++) {
            for (int j = 0; j < books.length-1-i; j++) {
                if (c.compare(books[j].getName().length(),books[j+1].getName().length()) > 0){
                    temp = books[j];
                    books[j] = books[j+1];
                    books[j+1] = temp;
                }
            }
        }
    }
    public static void AA(Book[] book, Comparator c) {
        Book temp;
        for (int i = 0; i < book.length - 1; i++) {
            for (int j = 0; j < book.length - 1 - i; j++) {
                if (c.compare(book[j].getPrice(), book[j + 1].getPrice()) > 0) {
                    temp = book[j];
                    book[j] = book[j + 1];
                    book[j + 1] = temp;
                }
            }

        }
    }
}

class Book {
    private String name;
    private double price;

    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}